package com.wenzi.rule.verification.fun;

import com.wenzi.rule.verification.annotate.Fun;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;
import org.springframework.util.ReflectionUtils;

import java.lang.reflect.Method;

@Component
public class FunBeanPostProcessor implements BeanPostProcessor {
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        ReflectionUtils.doWithMethods(bean.getClass(), new ReflectionUtils.MethodCallback() {
            @Override
            public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
                Fun fun = method.getAnnotation(Fun.class);
                if (fun != null){
                    //这里需要一个map将这些方法进行存储，方便后续spel解析时使用
                    FunRegister.register(fun.name(),method);
                }
            }
        });
        return bean;
    }
}
